package br.edu.universo.si.projeto2.infojob.client.componentes.dialogboxes;

import br.edu.universo.si.projeto2.infojob.client.util.Util;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.client.ui.FlexTable.FlexCellFormatter;


/**
 * COMPONENTE - Caixa de Dialogo de Confirmacao
 * 's' : quando o usuario clica no botao Sim
 * 'n' : quando o usuario clica no botao Nao
 * @author Andrey A. de Oliveira
 * @since 09/09/2008
 */
public class ConfirmationDialogBox extends DialogBox
{
	private final static String TITULO = "CONFIRMAÇÃO";
	private final static Image QUESTION_ICON = new Image(Util.getImagensPath() + "question_icon.png");
	
	private FlexTable flexTable;

	private HorizontalPanel rodape;

	private Button simBtn;
	private Button naoBtn;
	
	// interface interna estatica para retornar ao sistema qual opcao (sim ou nao) o usuario escolheu
	// meio complicado? o.O
	public static interface ConfirmCallback
	{
		void execute(String btnId);
	};

	public ConfirmationDialogBox(String texto, final ConfirmationDialogBox.ConfirmCallback cb)
	{
		super(false, true);

		this.setText(ConfirmationDialogBox.TITULO);
		this.setAnimationEnabled(true);

		flexTable = new FlexTable();
		flexTable.setCellSpacing(5);

		rodape = new HorizontalPanel();
		rodape.setSpacing(3);

		simBtn = new Button("Sim");
		naoBtn = new Button("Não");

		rodape.add(simBtn);
		rodape.add(naoBtn);

		FlexCellFormatter cellFormatter = flexTable.getFlexCellFormatter();
		cellFormatter.setAlignment(1, 0, HasHorizontalAlignment.ALIGN_CENTER, HasVerticalAlignment.ALIGN_MIDDLE);
		cellFormatter.setAlignment(1, 1, HasHorizontalAlignment.ALIGN_CENTER, HasVerticalAlignment.ALIGN_MIDDLE);

		// insere os componentes
		flexTable.setWidget(0, 0, ConfirmationDialogBox.QUESTION_ICON);
		flexTable.setWidget(0, 1, new HTML(texto));
		flexTable.setWidget(1, 0, rodape);
		
		flexTable.getFlexCellFormatter().setColSpan(1, 0, 2);

		this.add(flexTable);
		
		// action para o botao SIM
		simBtn.addClickListener(
			new ClickListener()
			{
				public void onClick(Widget sender)
				{
					cb.execute("s"); // a implementacao deste metodo se da na classe que invocou essa dialog box
					ConfirmationDialogBox.this.hide();
				}
			}
		);
		
		// action para o botao NAO
		naoBtn.addClickListener(
			new ClickListener()
			{
				public void onClick(Widget sender)
				{
					cb.execute("n"); // a implementacao deste metodo se da na classe que invocou essa dialog box
					ConfirmationDialogBox.this.hide();
				}
			}
		);
		
	}
	
	@Override
	public void show()
	{
		int left = (Window.getClientWidth() - this.getOffsetWidth()) / 2;
		int top = (Window.getClientHeight() - this.getOffsetHeight()) / 2;

		this.setPopupPosition(left, top);
        super.show();
	}
}